﻿using System;
using System.Threading;
using System.Threading.Tasks;

namespace APMDemo
{
    class Program
    {
        //static void Main()
        //{
        //    Console.WriteLine("Main ---> Start");
        //    Task t1 = Task1();
        //    Console.WriteLine("Main ---> Task1 END");
        //    Task<int> t2 = Task2();
        //    Console.WriteLine("Main ---> Task2 END");
        //    Task<int> t3 = Task3();
        //    Console.WriteLine("Main ---> Task3 END");
        //    //var r = Task3().Result;
        //    //Console.WriteLine(r);

        //    Console.WriteLine(t2.Result);
        //    Console.WriteLine(t3.Result);
        //    Console.ReadLine();
        //    Console.WriteLine("Main ---> End");
        //}

        static async Task Task1()
        {
            Console.WriteLine("Task1 ---> Start");
            Thread.Sleep(TimeSpan.FromSeconds(2));
            Console.WriteLine("Task1 ---> End");
        }

        static async Task<int> Task2()
        {
            Console.WriteLine("Task2 ---> Start");
            Thread.Sleep(TimeSpan.FromSeconds(3));
            //非async修饰的方法，必须使用Task.FromResult<TResult>()方法包装一个TASK对象返回
            Console.WriteLine("Task2 ---> End");
            return await Task.FromResult(5);
        }

        static async Task<int> Task3()
        {
            Console.WriteLine("Task3 ---> Start");
            int result = await Task2();
            Console.WriteLine("Task3 ---> End");
            return result * 100;
        }
    }
}
